<template>
	<view class="content">
		<!-- <goods-list :list="goodsList"></goods-list> -->
		<view class="padding">
			<!-- <view class="word_rl black textright padding_bottom" v-if="gouwcnr.length" >管理
			</view> -->
			<view class="zwtimg placeholderimg flexcenter" style="margin-top: 300rpx;" v-if="gouwcnr.length==0">
				<image src="@/static/image/bg-icons/placeholderimg.png" mode=""></image>
			</view>
			<view class="word_rl black textright " v-show="gouwcnr.length && guanliflag"
				@click="guanliflag=!guanliflag">取消管理</view>
			<view class="word_rl black textright " v-show="gouwcnr.length &&!guanliflag"
				@click="guanliflag=!guanliflag">管理</view>
			<view class="neirong">
				<!-- 空白页 -->
				<view class="zwtimg flexcenter" v-if="!gouwcnr.length">
					<image src="/static/image/home/placeholderimg.png" mode=""></image>
				</view>
				<view class="ffffff radius padding margin_top" v-else v-for="item in gouwcnr" :key="item.sku.id">
					<view class="flexbetween ">
						<view class="" style="width: 8%;" @click="selectitem(item)">
							<!-- 已勾选 -->
							<view class="xuanzhongs margin_right3" v-if="item.is_checked && Colortype==0"></view>
							<!-- 三个不同的颜色 -->
							<view class=" xuanzhong_blue margin_right3" v-if="item.is_checked && Colortype==3"></view>
							<view class=" xz_green margin_right3" v-if="item.is_checked && Colortype==1"></view>
							<view class=" xz_shen margin_right3" v-if="item.is_checked && Colortype==2"></view>
							<!-- 未勾选 -->
							<view class="wiexuanzhongs margin_right2" v-if="!item.is_checked"></view>
						</view>
						<view class="flextops" style="width: 91%;">
							<view class="shilitu  margin_right3"  @click="goGoodsInfo(item.goods_id)">
								<image :src="item.image" mode=""></image>
							</view>
							<view class="flexcolumn1" style="width: 63%;">
								<view class="text_e color_san yhxiank"  @click="goGoodsInfo(item.goods_id)">{{item.title}}</view>
								<view class="huieshs "  @click="goGoodsInfo(item.goods_id)">{{item.sku.difference}}</view>
								<view class="flexbetween " style="width: 100%;">
									<view class="qianshu money_qian" v-show="Colortype==0"  @click="goGoodsInfo(item.goods_id)">¥{{item.sku.price}}</view>
									<!-- 三个不同的颜色 -->
									<view class="darkblue_size money_qian" v-show="Colortype==2"  @click="goGoodsInfo(item.goods_id)">¥{{item.sku.price}}</view>
									<view class="blue_size money_qian" v-show="Colortype==3" @click="goGoodsInfo(item.goods_id)">¥{{item.sku.price}}</view>
									<view class="green_size money_qian" v-show="Colortype==1" @click="goGoodsInfo(item.goods_id)">¥{{item.sku.price}}</view>
									<view class="jiajian">
										<view class="jianhao" @click.stop="reduceBtn(item)">
											<!-- <view class="line"></view> -->
										</view>
										<!-- <view class="xhtext" v-show="Colortype==0">{{item.number}}</view> -->
										<input class="xhtext" type="number" v-show="Colortype==0" v-model="item.number" @blur="blurInput($event,item)">
										<!-- 三个不同的颜色 -->
										<!-- <view class="text_w darkblue_size" v-show="Colortype==2">{{item.number}}</view>
										<view class="text_w blue_size" v-show="Colortype==3">{{item.number}}</view>
										<view class="text_w green_size" v-show="Colortype==1">{{item.number}}</view> -->
										<input class="text_w darkblue_size text" style="width: 80rpx;" type="number" v-show="Colortype==2" v-model="item.number" @blur="blurInput($event,item)">
										<input class="text_w blue_size text" style="width: 80rpx;" type="number" v-show="Colortype==3" v-model="item.number" @blur="blurInput($event,item)">
										<input class="text_w green_size text" style="width: 80rpx;" type="number" v-show="Colortype==1" v-model="item.number" @blur="blurInput($event,item)">
										<view class="jiahao" @click.stop="addBtn(item)"></view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 固定底部 -->
			<view class="gudingdibu1 ffffff padding" v-if="gouwcnr.length">
				<view class="flexbetween">
					<view class="flexleft" @click="xiaoshi">
						<!-- 已勾选 -->
						<view class="xuanzhongs margin_right3" v-if="quan && Colortype==0"></view>
						<view class=" xuanzhong_blue margin_right3" v-if="quan && Colortype==3"></view>
						<view class=" xz_green margin_right3" v-if="quan && Colortype==1"></view>
						<view class=" xz_shen margin_right3" v-if="quan && Colortype==2"></view>
						<!-- 未勾选 -->
						<view class="wiexuanzhongs margin_right3" v-if="!quan"></view>
						
						<view class="titletext">全选</view>
					</view>
					<view class="btn line_orange" @click="buy" v-show="!guanliflag && Colortype==0">
						<view class="">立即支付</view>
					</view>
					<!-- 三个不同的颜色 -->
					<view class="btn line_green" @click="buy" v-show="!guanliflag && Colortype==1">
						<view class="">立即支付</view>
					</view><view class="btn line_blue" @click="buy" v-show="!guanliflag && Colortype==3">
						<view class="">立即支付</view>
					</view><view class="btn line_shen" @click="buy" v-show="!guanliflag && Colortype==2">
						<view class="">立即支付</view>
					</view>
					<!-- 移除购物车 -->
					<view class="btn line_orange" @click="scgwc" v-show="guanliflag && Colortype==0">
						<view class="">移出购物车</view>
					</view>
					<!-- 三个不同的颜色 -->
					<view class="btn line_green" @click="scgwc" v-show="guanliflag && Colortype==1">
						<view class="">移出购物车</view>
					</view><view class="btn line_blue" @click="scgwc" v-show="guanliflag && Colortype==3">
						<view class="">移出购物车</view>
					</view><view class="btn line_shen" @click="scgwc" v-show="guanliflag && Colortype==2">
						<view class="">移出购物车</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import goodsList from "@/components/goodList.vue"
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		components: {
			goodsList
		},
		data() {
			return {
				goodsList: [],
				quan: false,
				gouwcnr: [], //购物车列表内容
				allprice: 0,
				guanliflag: false,
				Colortype: 0, //0=褐色,1=绿色,2=深蓝,3=浅蓝
				is_login:false,
			}
		},
		onLoad() {
			this.Colortype = this.$store.state.type
		},
		onShareAppMessage: function(options) {
			return {
				title: '工蜂商城',
				imageUrl: '@/static/logo.png',
				path: "pages/index/index?rec_uid=" + uni.getStorageSync("user_id"),
			}
		},
		onShareTimeline: function() {
			return {
				title: '工蜂商城',
				imageUrl: '@/static/logo.png',
				path: "pages/index/index?rec_uid=" + uni.getStorageSync("user_id"),
			}
		},
		onShow() {
			this.is_login = httpRequest.checkIsLogin();
			if (this.is_login) {
				this.gwclb()
			}else{
			}
			
		},
		watch: {
			gouwcnr: {
				handler(newValue, oldValue) {
					let isall_check = 0
					let num = 0
					newValue.forEach(item => {
						if (item.is_checked) {
							num = num + Number(item.sum) * Number(item.number)
						} else {
							isall_check = isall_check + 1
						}
					})
					this.allprice = num
					if (isall_check == 0) {
						this.quan = true
					} else {
						this.quan = false
					}
				},
				deep: true
			}
		},
		methods: {
			buy() {
				let obj = {
					data: [],
					sourcetype: 2, //1商品详情，2购物车
				}
				let num = 0
				this.gouwcnr.forEach(item => {
					if (item.is_checked) {
						obj.data.push({
							goods_id: item.goods_id,
							sku_id: item.sku.id,
							number: item.number,
						})
						num = num + 1
					}
				})
				if (num == 0) {
					httpRequest.toast("请选择商品")
					return
				}
				uni.navigateTo({
					url: "/pages_index/querendd?msg=" + encodeURIComponent(JSON.stringify(obj))
				})
			},
			// 跳转商品详情
			goGoodsInfo(id) {
				uni.navigateTo({
					url: "/pages_index/shangpinxq?id=" + id
				})
			},
			// 删除购物车
			scgwc() {
				let data = []
				this.gouwcnr.forEach(item => {
					if (item.is_checked) {
						data.push({
							goods_id: item.goods_id,
							sku_id: item.sku.id
						})
					}
				})
				httpRequest.request('api/cart/delCart', 'POST', {
					data
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						this.gwclb()
					}
				})
			},
			// 购物车列表接口
			gwclb() {
				httpRequest.request('api/cart/cartList', 'POST', {
					// 传参
				}, false, false, true).then(res => {
					if (res.code == 1) {
						res.data.forEach(item => {
							this.$set(item, "is_checked", false)
						})
						this.gouwcnr = res.data
					}
				})
			},
			// 减
			reduceBtn(item) {
				if (item.number != 1) {
					item.number = Number(item.number) - 1
					httpRequest.request('api/cart/clCart', 'POST', {
						goods_id: item.goods_id,
						sku_id: item.sku.id,
						number: item.number
					}, false, false, true).then(res => {})
				}

			},
			blurInput(e,item){
				// console.log(e);
				// console.log(item);
				this.addBtn(item)
			},
			// 加
			addBtn(item) {
				// if(item.number==10){
				if(item.sku.stock==item.number){
					httpRequest.toast("没有那么多库存呢，亲！")
					return false
				}
				item.number = Number(item.number) + 1
				httpRequest.request('api/cart/clCart', 'POST', {
					goods_id: item.goods_id,
					sku_id: item.sku.id,
					number: item.number
				}, false, false, true).then(res => {
					if(res.code==0){
						httpRequest.toast(res.msg)
					}
					this.gwclb()
				})
				// }
			},
			selectitem(item) {
				item.is_checked = item.is_checked ? false : true
			},
			// 立即购买
			atonceBuy() {
				let data = {
					msg: [],
					type: "cart" //car购物车，detail详情
				}
				this.gouwcnr.forEach(item => {
					if (item.is_checked) {
						data.msg.push({
							goodsId: item.id,
							specType: 0,
							num: item.number
						})
					}
				})
				if (data.msg.length == 0) {
					return
				}
				uni.navigateTo({
					url: '/pages/gouwuche/querendd?msg=' + encodeURIComponent(JSON.stringify(data))
				})

			},
			xiaoshi() {
				if (this.quan) {
					this.quan = false
					this.gouwcnr.forEach(item => {
						item.is_checked = false
					})
				} else {
					this.quan = true
					this.gouwcnr.forEach(item => {
						item.is_checked = true
					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.btn {
		padding: 20rpx 50rpx;
		border-radius: 120rpx;
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
	}

	.line_orange {
		background-color: rgba(212, 139, 57, 1);
	}

	.line_blue {
		background-color: rgba(20, 132, 198, 1);
	}

	.line_green {
		background-color: rgba(27, 179, 147, 1);
	}

	.line_shen {
		background-color: RGBA(27, 81, 120, 1);
	}

	.neirong {
		padding-bottom: 244rpx;
	}

	.huieshs {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.gudingdibu1 {
		bottom: 0;
		position: fixed;
		left: 0;
		width: 100%;
		box-sizing: border-box;
	}

	.jiahao {
		background-size: 100% 100%;
		width: 21rpx;
		height: 19rpx;
		position: absolute;
		top: 14rpx;
		right: 12rpx;

	}

	.jianhao {
		width: 22rpx;
		position: absolute;
		left: 13rpx;
		// height: 3.5rpx;
		top: 20rpx;
		height: 20rpx;

		.line {
			height: 3.5rpx;
			width: 22rpx;
			background-color: #999999;
		}
	}

	.xhtext {
		width: 80rpx;
		text-align: center;
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: RGBA(255, 117, 16, 1);
	}

	.jiajian {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 164rpx;
		height: 46rpx;
		position: relative;
		border-radius: 20rpx;
		background-size: 100% 100%;
		background-image: url('/static/image/home/bujinqi.png');
	}

	.qianshu {
		color: #D48B39;
	}

	.money_qian {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
	}

	.shilitu {
		width: 196rpx;
		height: 188rpx;
		border-radius: 14rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 14rpx;
		}
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
	}

	.yhxiank {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden; //溢出内容隐藏
		text-overflow: ellipsis; //文本溢出部分用省略号表示
		display: -webkit-box; //特别显示模式
		-webkit-line-clamp: 2; //行数
		line-clamp: 2;
		-webkit-box-orient: vertical; //盒子中内容竖直排列
	}

	.flexcolumn1 {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		flex-direction: column;
		height: 200rpx;
	}
</style>